#!/bin/ash -efu

normalize() {
	sed \
		-e "s/\"/'/g" \
		-e 's/[[:space:]]\+/ /g' \
		-e 's/^[[:space:]]\+//' \
		-e 's/[[:space:]]\+$//'
}

getopt_test001() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -n TEST -o -- || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -n TEST -o -- || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}

getopt_test002() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -n TEST -o ' ' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -n TEST -o ' ' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}

getopt_test003() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -n TEST -o '' -l ' ' -- ZZZ || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -n TEST -o '' -l ' ' -- ZZZ || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}


getopt_test004() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -n TEST -o 'a,b,c' -- -abc -a -b -c || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -n TEST -o 'a,b,c' -- -abc -a -b -c || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}

getopt_test005() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -n TEST -o 'a,b,c' -l 'caa:,cba:' -- -abc --caa 'AAA' --cba='BBB' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -n TEST -o 'a,b,c' -l 'caa:,cba:' -- -abc --caa 'AAA' --cba='BBB' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}

getopt_test006() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -n TEST -o 'a,b,c' -l 'caa:,cba:' -- --cb 'AAA' --c='BBB' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -n TEST -o 'a,b,c' -l 'caa:,cba:' -- --cb 'AAA' --c='BBB' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}

getopt_test007() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -n TEST -o 'a,b,c' -l 'caa:,cba:' -- --cbz 'AAA' --c='BBB' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -n TEST -o 'a,b,c' -l 'caa:,cba:' -- --cbz 'AAA' --c='BBB' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}

getopt_test008() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -a -n TEST -o 'a,b,c' -l 'caa:,cba:' -- -cb 'AAA' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -a -n TEST -o 'a,b,c' -l 'caa:,cba:' -- -cb 'AAA' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}

getopt_test009() { # UnitTest
	LANG=C
	expect="TEST: option '-c=AAA' is ambiguous; possibilities: '--caa' '--cba';--;rc=1;"
	result=`{ rc=0; getopt -a -n TEST -o 'a,b,c' -l 'caa:,cba:' -- -c='AAA' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	[ "$result" = "$expect" ] ||
		expect="TEST: option '-c=AAA' is ambiguous; possibilities: '-caa' '-cba';--;rc=1;"
	assertEquals "unexpected output from $(getopt --version): $result" "$result" "$expect"
	. ../shell-getopt
	expect="TEST: option '-c=AAA' is ambiguous; possibilities: '--caa' '--cba';--;rc=1;"
	result=`{ rc=0; getopt -a -n TEST -o 'a,b,c' -l 'caa:,cba:' -- -c='AAA' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}

getopt_test010() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -n TEST -o 'a,b,c:' -l 'caa:,cba:' -- -c='AAA' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -n TEST -o 'a,b,c:' -l 'caa:,cba:' -- -c='AAA' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}

getopt_test011() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -n TEST -o 'a,b,c' -l 'abcd:,cbce:' -- --abcx 'AAA' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -n TEST -o 'a,b,c' -l 'abcd:,cbce:' -- --abcx 'AAA' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}

getopt_test012() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -n TEST -o 'a:,b,c' -- -abc 'AAA' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -n TEST -o 'a:,b,c' -- -abc 'AAA' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}

getopt_test013() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -n TEST -o 'a::,b,c' -- -a -bc 'AAA' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -n TEST -o 'a::,b,c' -- -a -bc 'AAA' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}

getopt_test014() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -n TEST -o 'a,b,c' -l 'caa:,cba:' -- 'BBB' 'XXX ZZZ' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -n TEST -o 'a,b,c' -l 'caa:,cba:' -- 'BBB' 'XXX ZZZ' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}

getopt_test015() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -n TEST -o '+a,b,c' -l 'caa:,cba:' -- --caa='AAA' 'BBB' --zzz -x || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -n TEST -o '+a,b,c' -l 'caa:,cba:' -- --caa='AAA' 'BBB' --zzz -x || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}


getopt_test016() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -n TEST -o '-a,b,c' -l 'caa:,cba:' -- 'AAA' --caa='BBB' 'CCC' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -n TEST -o '-a,b,c' -l 'caa:,cba:' -- 'AAA' --caa='BBB' 'CCC' || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}

getopt_test017() { # UnitTest
	LANG=C
	. ../shell-getopt
	expect=' -a -b -- 
 -a -b -- 
 -a -b -- '
	result=`for i in 1 2 3; do getopt -n TEST -o 'a,b' -- -ab; done 2>&1`
	assertEquals "$result" "$result" "$expect"
}

getopt_test018() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -n TEST -o '' -l 'tag:,tag-message:' -- ZZZ --tag=xxx || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -n TEST -o '' -l 'tag:,tag-message:' -- ZZZ --tag=xxx || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}

getopt_test019() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -n TEST -o '' -l 'tag-message:,tag:' -- ZZZ --tag=xxx || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -n TEST -o '' -l 'tag-message:,tag:' -- ZZZ --tag=xxx || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}

getopt_test020() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -n TEST -o '' -l 'tag-message:,tag:,tag-subject:' -- ZZZ --tag=xxx || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -n TEST -o '' -l 'tag-message:,tag:,tag-subject:' -- ZZZ --tag=xxx || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}

getopt_test021() { # UnitTest
	LANG=C
	expect=`{ rc=0; getopt -n TEST -o '' -l 'tag-message:,tag:,tag-subject:' -- "Z Z" --tag="x x" || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	. ../shell-getopt
	result=`{ rc=0; getopt -n TEST -o '' -l 'tag-message:,tag:,tag-subject:' -- "Z Z" --tag="x x" || rc=$?; echo rc=$rc; } 2>&1 |normalize |tr '\n' ';'`
	assertEquals "$result" "$result" "$expect"
}
